package com.atguigu.mall.mallproduct.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.mall.mallproduct.dao.PmsCategoryDao;
import com.atguigu.mall.mallproduct.entity.PmsCategoryEntity;
import com.atguigu.mall.mallproduct.service.PmsCategoryService;

@Service("pmsCategoryService")
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryDao, PmsCategoryEntity>
    implements PmsCategoryService {

  private HashMap<String, Object> cache = new HashMap<>();

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<PmsCategoryEntity> page =
        this.page(
            new Query<PmsCategoryEntity>().getPage(params), new QueryWrapper<PmsCategoryEntity>());

    return new PageUtils(page);
  }

  @Override
  public List<PmsCategoryEntity> listWithTree() {

    // 1.查出所有的分类
    List<PmsCategoryEntity> entities = baseMapper.selectList(null);

    // 2.组装成父子的树结构
    // 2.1.找出所有的一级分类
    List<PmsCategoryEntity> levelMenus =
        entities.stream()
            .filter(category -> category.getParentCid() == 0)
            .map(
                menu -> {
                  menu.setChildren(getChildren(menu, entities));
                  return menu;
                })
            .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
            .collect(Collectors.toList());
    return levelMenus;
  }

  @Override
  public void removeMenuByIds(List<Long> asList) {

    // TODO 1.检查当前的菜单，是否被别的地方引用
    baseMapper.deleteBatchIds(asList);
  }

  @Override
  public Long[] findCatelogPath(Long catelogId) {
    List<Long> paths = new ArrayList<>();
    List<Long> parentPath = findParentPath(catelogId, paths);
    Collections.reverse(parentPath);
    return (Long[]) paths.toArray(new Long[parentPath.size()]);
  }

  private List<Long> findParentPath(Long catelogId, List<Long> paths) {
    // 1.收集当前节点id
    paths.add(catelogId);
    PmsCategoryEntity byId = this.getById(catelogId);
    if (byId.getParentCid() != 0) {
      findParentPath(byId.getParentCid(), paths);
    }
    return paths;
  }

  /**
   * 递归查找所有菜单的子菜单
   *
   * @param root
   * @param all
   * @return
   */
  private List<PmsCategoryEntity> getChildren(PmsCategoryEntity root, List<PmsCategoryEntity> all) {

    List<PmsCategoryEntity> children =
        all.stream()
            .filter(
                category -> {
                  return category.getParentCid() == root.getCatId();
                })
            .map(
                category -> {
                  category.setChildren(getChildren(category, all));
                  return category;
                })
            .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
            .collect(Collectors.toList());
    return children;
  }
}
